<?php

namespace app\services;

use GuzzleHttp\Client;
use plugin\admin\app\model\Order;
use support\Cache;

class RequestService
{
    private static $appId = '90F4F7FC5562A459D98F1C6B0FB82CF6';
    private static $appKey = '1124407150980267679';
    public static function client($url,$jsonData)
    {
        $result = Cache::get($url.$jsonData);
        if (empty($result)){
            $client = new Client();
            $signature = strtoupper(md5(self::$appKey.$jsonData));
            $requestUrl = 'https://area82-win.pospal.cn:443/'.$url;

            // 生成毫秒级时间戳
            $timestamp = (int)(microtime(true) * 1000);
            $request = $client->request('POST', $requestUrl, [
                'headers' => [
                    'User-Agent' => 'openApi',
                    'Content-Type' => 'application/json; charset=utf-8',
                    'accept-encoding' => 'gzip,deflate',
                    'time-stamp' => $timestamp,
                    'data-signature' => $signature
                ],
                'json' => json_decode($jsonData, true),
            ]);
            $result = $request->getBody()->getContents();
            Cache::set($url.$jsonData,$result,600);
        }

        return $result;
    }

    // 变更会员余额
    public static function updateBalance($appId,$customerUid,$balanceIncrement,$dataChangeTime)
    {
        $result = self::client('pospal-api2/openapi/v1/customerOpenApi/updateBalancePointByIncrement',json_encode([
                'appId' => $appId,
                'customerUid'=> $customerUid,
                'balanceIncrement' => $balanceIncrement,
                'dataChangeTime' => $dataChangeTime
            ])
        );
        return json_decode($result,true);
    }

   // 查询网络订单信息
    public static function orderInfo($appId,$orderNo)
    {
        $result = Cache::get('order_info_'. $orderNo);
        if (empty($result)){
            $result = self::client('pospal-api2/openapi/v1/orderOpenApi/queryOrderByNo',json_encode([
                    'appId' => $appId,
                    'orderNo' => $orderNo,
                ])
            );
        }
        $data = json_decode($result,true);
        if (!empty($data['status']) && $data['status'] == 'success'){
            Cache::set('order_info_'. $orderNo, $result, 6000);
        }
        return $data;
    }

    // 查询网络订单信息
    public static function ticketInfo($appId,$orderNo)
    {
        $result = Cache::get('ticket_info_'. $orderNo);
        if (empty($result)){
            $result = self::client('pospal-api2/openapi/v1/ticketOpenApi/queryTicketBySn',json_encode([
                    'appId' => $appId,
                    'sn' => $orderNo,
                ])
            );
        }
        $data = json_decode($result,true);
        if (!empty($data['status']) && $data['status'] == 'success'){
            Cache::set('ticket_info_'. $orderNo, $result, 6000);
        }
        return $data;
    }

    // 根据会员号查询会员信息
    public static function customerInfoByNumber($appId,$number)
    {
        $result = self::client('pospal-api2/openapi/v1/customerOpenApi/queryByNumber',json_encode([
                'appId' => $appId,
                'customerNum' => $number,
            ])
        );
        $result = json_decode($result,true);
        return $result;
    }

    public static function customerInfoById($appId,$id)
    {
        $result = self::client('pospal-api2/openapi/v1/customerOpenApi/queryByUid',json_encode([
                'appId' => $appId,
                'customerUid' => $id,
            ])
        );
        $result = json_decode($result,true);
        return $result;
    }

    // 根据条形码批量查询商品信息
    public static function queryProductByBarcodes($barcodes,$appId = '')
    {
        if (empty($appId)){
            $appId = self::$appId;
        }
        $result = self::client('pospal-api2/openapi/v1/productOpenApi/queryProductByBarcodes',json_encode([
                'appId' => $appId,
                'barcodes' => $barcodes,
            ])
        );
        $result = json_decode($result,true);
        return $result;
    }

    public static function queryMyPayMethod($appId)
    {
        if (empty($appId)){
            $appId = self::$appId;
        }
        $result = self::client('pospal-api2/openapi/v1/ticketOpenApi/queryMyPayMethod',json_encode([
                'appId' => $appId,
            ])
        );
        $result = json_decode($result,true);
        return $result;
    }

    public static function priceFormat($price)
    {
        return str_replace('.00', '', $price);
    }


}